package com.ciaojian.worker.service;

import com.ciaojian.core.model.HairArchive;
import com.ciaojian.core.model.HairArchiveImg;
import com.ciaojian.core.service.HairArchiveImgService;
import com.ciaojian.core.service.HairArchiveService;
import com.ciaojian.worker.model.param.HairArchiveParam;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author oxiny
 */
@Service
@AllArgsConstructor
public class HairArchiveImgServiceImpl extends HairArchiveImgService {
    private final HairArchiveService hairArchiveService;

    /**
     * 用户最近发型
     *
     * @param userId /
     * @return /
     */
    public List<Map<String, Object>> recurrently(Integer userId) {
        return this.baseMapper.recurrently(userId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(HairArchiveParam hairArchiveParam) {
        // 先插入主表
        HairArchive hairArchive = hairArchiveParam.convert();
        hairArchive.insert();

        List<HairArchiveImg> hairArchiveImgList = new ArrayList<>();
        hairArchiveParam.getImages().forEach(image -> {
            image.setArchiveId(hairArchive.getId());
            hairArchiveImgList.add(image.convert());
        });

        this.getBaseMapper().insertBatch(hairArchiveImgList);
    }
}
